package com.businessportal.web.controllers.calendar;

import com.businessportal.web.dataaccess.entities.Event;
import com.businessportal.web.dataaccess.entities.Login;
import com.businessportal.web.dataaccess.services.EventService;
import com.businessportal.web.dataaccess.services.UserService;
import org.json.simple.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by Vadim on 7/5/14.
 */

@WebServlet(urlPatterns = "/securepages/calendar_deleter.do")
public class CalendarDeleterController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("application/json");
        JSONObject retVal=new JSONObject();

        try{
            req.setCharacterEncoding("UTF-8");

            String eventId = req.getParameter("eventId");

            HttpSession session = req.getSession();
            Login user = (Login)session.getAttribute("user");

            if(null != eventId && !eventId.isEmpty()){
                EventService.INSTANCE.deleteById(Long.parseLong(eventId));
                retVal.put("message", "OK");
            } else {
                retVal.put("message", "Not found");
            }

        } catch (Exception e) {
            retVal.put("error", e.getMessage());
        }

        resp.getWriter().write(retVal.toString());
    }
}
